<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>项目列表</title>
        <link href="../css/mems-index.css" type="text/css" rel="stylesheet"/>
        <link rel="stylesheet" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css">
        <link href="../css/foundation-datepicker.css" rel="stylesheet"/>
    </head>
<body>
<!--header-->
<header id="header" class="header">
    <div class="header-info left"><i></i><a href="mems-pro-list-myself.html">项目列表</a></div>
    <div class="right-user right">
         <div class="clear t" >             <div id="headusername" class="role"></div>             <div class="icon"></div>         </div>
        <div class="head-down">
            <ul><li><a href="edit-pwd.html">修改密码</a></li><li><a onclick="loginOut()">退出系统</a></li></ul></div><div>
    </div>
    </div>
</header>
<!--menu-->
<aside id="aside" class="aside">
    <div class="logo">运营平台</div>
    <ul class="menu" id="asidemenu">
    </ul>
</aside>


<!--content-->
<div class="content">
    <div class="page-inner" style="padding-top: 13px">
    <ul class="item-tab clear">
        <li><a href="mems-pro-list-myself.html" class="current">我的项目</a></li>
        <li><a href="mems-pro-list.html">全部项目</a></li>
        </ul>
        <table class="mems-search">
            <tbody>
                <tr>
                    <td>&emsp;&emsp;项目名称：<input id="needSource"/></td>
                    <td>&emsp;&emsp;&emsp;&emsp;segmentation：<select id="Segmentation"></select></td>
                    <td>&emsp;&emsp;级别：<select id="level"></select></td>
                    <td>商务阶段：<select id="bussType"></select></td>
                    <td align="right"><button class="btn" id="checkout">查询</button></td>
                </tr>
            </tbody>

            <tbody id="more-search" >
                <tr>
                    <td colspan="5">&emsp;&emsp;&emsp;&emsp;区域：<select id="district"><option>请选择</option></select>&emsp;<select style="width: 200px" id="privence"><option>请选择</option></select>&emsp;<select id="city"><option>请选择</option></select></td>
                </tr>
            </tbody>
        </table>
        <div class="commit-new-item"><a class="btn" href="mems-pro-creat-1.html" id="creatProId" >创建项目&nbsp;<b>+</b></a>&emsp;<a class="btn" onclick="exportExcel();" id="exportExcelId">导出&nbsp;</a></div>
        
        <div class="item-content">
            <div class="table-box">
                <table>
                    <thead>
                        <tr class="odd">
                            <!--<th><input type="checkbox" name="" id="checkAll">全选</th>-->
                            <th>ID</th>
                            <th>项目</th>
                            <th>负责人</th>
                            <th>区域</th>
                            <th>segmentation</th>
                            <th>级别</th>
                            <th>商务阶段</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="bodyList">

                    </tbody>
                </table>
            </div>
            <div class="pages clear">
                <div class="page-list clear" id="pageList">
                </div>
            </div>
        </div>
    </div>

</div>
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.cookie.js"></script>
<script src="../js/common.js"></script>
<script src="../js/foundation-datepicker.min.js"></script>
<script src="../js/common.js"></script>
<script>
    var roleid = $.cookie('userrole');
    //权限控制
    if(roleid == 0){
        $('#creatProId').remove()
    }
    if(roleid == 0 || roleid == 2){
        $('#exportExcelId').remove()
    }

    $('#checkAll').on('click',function () {
        var checked = $(this).prop('checked');
        var checkbox = $('#bodyList').find('input[type=checkbox]');
        if(checked){
            checkbox.each(function () {
                $(this).prop('checked',true);
            })
        }else {
            checkbox.each(function () {
                $(this).prop('checked',false);
            })
        }
    })

    function getData(p,checPara,sessionPara){
        var PerPage = 20;
        var url  = _ip+'/project/getProjectList?page='+p+'&size='+PerPage;
        var data = {};
        if(checPara){
            var para = {
                "name": $('#needSource').val() || null,
                "segmentation": $('#Segmentation').val() || null,
                "level": $('#level').val() || null,
                "businessType": $('#bussType').val() || null,
                "area": $('#district').find('option:selected').text()=='请选择'?null:$('#district').val(),
                "province": $('#privence').find('option:selected').text()=='请选择'?null : $('#privence').val(),
                "city": $('#city').find('option:selected').text()=='请选择'?null : $('#city').val()
            }
            data = para
        }
        if(sessionPara){
            data = sessionPara;
        }
        data['ownerId'] = $.cookie('userid');
        $.ajax({
            url:url,
            type : "POST",
            dataType: 'json',

            xhrFields: {
               withCredentials: true
            },
            crossDomain: true,

            contentType:'application/json',
            data:JSON.stringify(data),
            success:function(json){

                if(json.message === 'LOGIN') {
                    window.location.href = 'user-login.html';
                    return false;
                }
                var storage = {
                    p:p,
                    para:data
                }
                sessionStorage.setItem('myPro', JSON.stringify(storage));
                if(!json.data || json.data.length <= 0){
                    $('#bodyList').html('<tr><td colspan="8">暂无数据！</td></tr>');
                    $('#pageList').html('');
                    return false;
                }
                var _data     = [];
                var _pager    = [];
                var totalPage = json.totlePage;
                var prev      = p-1;
                if(prev < 1)prev = 1;
                var next = p + 1;
                if(next > totalPage)next = totalPage;
                for(var k = 0, kk = json.data.length; k < kk; k++){
                    var msg = json.data[k];

                    _data.push('<tr>');
//                    _data.push('<td><input type="checkbox" name=""></td>');
                    _data.push('<td>'+ msg.id +'</td>');
                    _data.push('<td><a href="mems-pro-detail-baseinfo.html?id='+msg.id+'&name='+msg.name+'" class="link" title="'+msg.name+'">'+(msg.name.length<12?msg.name:msg.name.substring(0,12)+'...' )+'</a></td>');
                    _data.push('<td>'+msg.ownerName+'</td>')
                    _data.push('<td>'+msg.area+msg.province+msg.city+'</td>');
                    _data.push('<td>'+msg.segmentation+'</td>');
                    _data.push('<td>'+msg.level+'</td>');
                    _data.push('<td>'+msg.businessType+'</td>');

                    if(roleid==9 || roleid==3){
                        _data.push('<td><a class="link" href="javascript:" onclick="deleteList('+msg.id+')">删除</a></td>');
                    }else{
                        _data.push('<td></td>');
                    }
                
                }
                _pager.push('<span>共：'+ totalPage +'页</span>');
                _pager.push('<span>您正在第：'+p+'页</span>')
                _pager.push('<a href="javascript:;" class="page-pre" onclick="getData('+prev+',true'+');return !1;">上一页</a>');
                _pager.push('<a href="javascript:;" class="page-next" onclick="getData('+next+',true'+');return !1;">下一页</a>');
                _pager.push('<span>前往第</span>');
                _pager.push('<input type="text" id="goToPage"/>');
                _pager.push('<span>页</span>');
                _pager.push('<button id="goToPageBtn">GO</button>');
                $('#bodyList').html(_data.join(""));
                $('#pageList').html(_pager.join(""));
                $('#goToPageBtn').on('click',function () {
                    var v = $('#goToPage').val();
                    if(!/^[1-9]\d*$/.test(v)){
                        return false;
                    }
                    if(v>1 && v<totalPage){
                        getData(v,true);
                    }
                    if(v <= 1){
                        getData(1,true);
                        return false;
                    }
                    if(v >= totalPage){
                        getData(totalPage,true);
                        return false;
                    }
                })
            }
        })
    }
    $('#checkout').on('click',function(){
        getData(1,true);
    })
    function init() {
        _API.dynamicDistrick('district','privence','city');
        var sessionPage = JSON.parse(sessionStorage.getItem('myPro')) || null;
        if(sessionPage){
            var p = sessionPage.p;
            var para = sessionPage.para;
            getData(p,false,para);
            $('#needSource').val(sessionPage.para.name);
            _API.getProductList('productList',sessionPage.para.product,true).getIssueStatusList('issueStatusList',sessionPage.para.status,true);
            _API.getPSegmentationList('Segmentation',sessionPage.para.segmentation,true)
                    .getPLevelList('level',sessionPage.para.level,true)
                    .getBussTypeList('bussType',sessionPage.para.businessType,true)

            if(sessionPage.para.area){
                _API.getDistrict('district',sessionPage.para.area,true)
                        .getPrivence('privence',sessionPage.para.province,true,sessionPage.para.area)
                        .getCity('city',sessionPage.para.city,true,sessionPage.para.area,sessionPage.para.province);
            }
        }else{
            getData(1);
            _API.getPSegmentationList('Segmentation',0,true)
                    .getPLevelList('level',0,true)
                    .getBussTypeList('bussType',0,true)
        }
    }
    init()
    function  deleteList(id) {
        if(confirm("该项目下所有相关的内容将被删除！是否删除？")){
            var url = _ip + '/project/delete?id='+id;
            $.ajax({
                url:url,
                type: 'GET',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                dataType: 'json',
                contentType:'application/json',
                jsonpCallback: 'login',
                success: function(json) {
                    if(!json.success){
                        alert(json.message);
                    }else{
                        getData(1)
                    }
                }
            })
        }
    }

    var nowTemp = new Date();
    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
    var checkin = $('#dpd1').fdatepicker({
        onRender: function (date) {
            return date.valueOf() < now.valueOf() ? '' : '';
        },
        // pickTime: true,
        format: 'yyyy-mm-dd'
    }).on('changeDate', function (ev) {
        if (ev.date.valueOf() > checkout.date.valueOf()) {
            var newDate = new Date(ev.date)
            newDate.setDate(newDate.getDate() + 1);
            checkout.update(newDate);
        }
        checkin.hide();
        $('#dpd2')[0].focus();
    }).data('datepicker');
    var checkout = $('#dpd2').fdatepicker({
        onRender: function (date) {
            return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
        },
        // pickTime: true,
        format: 'yyyy-mm-dd'
    }).on('changeDate', function (ev) {
        checkout.hide();
    }).data('datepicker');





    // exportExcel()
    function exportExcel(){
        var html = '';
        html += '<div class="share-box">';
            html += '<p class="relative-title">导出列表</p>';
            html += '<div class="con-box">';
               
                html += '<table class="exportItem" style="width:92%;height:50px;margin:0 auto">';
                    html += '<tr><th align="left" colspan="2">拜访信息</th></tr>';
                    html += '<tr height="50"><td width="100" align="right">拜访方式：</td><td><select id="visitType"></select></td></tr>';
                    html += '<tr height="50"><td align="right">拜访结果：</td><td><select id="exvisitResult"></select></td>';
                    html += '<tr height="50"><td align="right">紧急程度：</td><td><select id="exemergency"></select></td></tr>';
                    html += '<tr><th align="left" colspan="2">拜访信息</th></tr>';
                    html += '<tr height="50" align="right"><td>预计采购时间：</td><td ALIGN="left"><input id="procurementTime"/></td></tr>';
                html += '</table>';
            html += '</div>';
            html += '<div class="buttons"><a class="cancel" href="javascript:$$.closeLayer()">取消</a><a id="confirmRelative" class="confirm">确认</a></div>'
        html += '</div>';


        $$.layer(html, {
            isShowMask: true,
            fixedBoxTop:'100px',
            zIndex:99,
            contentBoxWidth: "30%"
        });

        _API.getVisitTypeList('visitType',0,true)
        .getEmergencyList('exemergency',0,true)
        .getVisitResultList('exvisitResult',0,true)

        var isClick = false;
        $('#confirmRelative').on('click',function(){
            if(isClick) return;
            $(this).addClass('sending');
            isClick = true;
            var url = _ip + '/customer/exportParams';
            var data = {
                "visitType":$("#visitType").find('option:selected').text() == '请选择'?null:$("#visitType").val(),
                "emergency":$("#exemergency").find('option:selected').text() == '请选择'?null:$("#exemergency").val(),
                "visitResult":$("#exvisitResult").find('option:selected').text() == '请选择'?null:$("#exvisitResult").val(),
                "date": $('#procurementTime').val()==""?null: $('#procurementTime').val(),
                "name": $('#needSource').val() || null,
                "segmentation": $('#Segmentation').val() || null,
                "level": $('#level').val() || null,
                "businessType": $('#bussType').val() || null,
                "area": $('#district').find('option:selected').text()=='请选择'?null:$('#district').val(),
                "province": $('#privence').find('option:selected').text()=='请选择'?null : $('#privence').val(),
                "city": $('#city').find('option:selected').text()=='请选择'?null : $('#city').val()
            }
            $.ajax({
                url:url,
                type: 'post',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                dataType: 'json',
                contentType:'application/json',
                data:JSON.stringify(data),
                success: function(json) {
                    if(json.message === 'LOGIN') {
                        window.location.href = 'user-login.html';
                        return false;
                    }
                    if(json.success){
                        location.href = _ip+'/customer/exportReport';
                        $$.closeLayer()
                    }else{
                        alert('添加失败，稍后重试！')
                    }
                }
            })
        })



        var nowTemp = new Date();
        var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
        $('#procurementTime').fdatepicker({
            format: 'yyyy-mm',
            onRender: function (date) {
                return date.valueOf() < now.valueOf() ? 'disabled' : '';
            }
        });
    }

</script>
</body>
</html>